package com.jpx.jxcbiz.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.common.dto.SaleDto;
import com.jpx.common.vo.CarListDto;
import com.jpx.common.vo.ResultEntity;
import com.jpx.common.vo.SalaListDto;
import com.jpx.jxcbiz.entity.TSaleList;
import com.jpx.jxcbiz.entity.TSaleListGoods;
import com.jpx.jxcbiz.entity.TUser;
import com.jpx.jxcbiz.service.ITSaleListGoodsService;
import com.jpx.jxcbiz.service.ITSaleListService;
import com.jpx.jxcbiz.service.ITUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */

@RestController
@RequestMapping("/biz/tSaleList")
@Slf4j
public class TSaleListController {

    @Autowired
    ITSaleListService itSaleListService;

    @Autowired
    ITUserService itUserService;

    @Autowired
    ITSaleListGoodsService itSaleListGoodsService;

    /**
     * 销售商品订单列表以及查询
     * @param carListDto
     * @return
     */
    @RequestMapping("/getSaleList")
    public ResultEntity getSaleList(CarListDto carListDto){
        Wrapper wrapper = new EntityWrapper();


        if(carListDto.getBname()!=null && carListDto.getBname()!=""){
            wrapper.eq("bname",carListDto.getBname());
        }
        if(carListDto.getSaleNumber()!=null && carListDto.getSaleNumber()!=""){
            wrapper.eq("sale_number",carListDto.getSaleNumber());
        }
        if(carListDto.getState()!=null){
            wrapper.eq("state",carListDto.getState());
        }
        if(carListDto.getStartTime()!=null && carListDto.getEndTime()!=null){
            wrapper.between("sale_date",carListDto.getStartTime(),carListDto.getEndTime());
        }

        List<CarListDto> carList = itSaleListService.getCarList(carListDto);

        return ResultEntity.ok(carList);
    }

    /**
     * 根据销售订单Id查询出销售订单
     * @param id
     * @return
     */
    @RequestMapping("/getSaleById")
    public ResultEntity getSaleById(int id){
        return ResultEntity.ok(itSaleListService.getSaleListById(id));
    }

    /**
     * 根据销售订单Id删除订单以及删除具体商品
     * @param id
     * @return
     */
    @RequestMapping("/deleteSaleListById")
    public ResultEntity deleteSaleListById(int id){
        //删除销售订单具体商品
        itSaleListService.deleteSaleListGoodsById(id);
        //删除销售订单
        itSaleListService.deleteSaleListById(id);
        return ResultEntity.ok();
    }

    @RequestMapping("/salaListSave")
    public ResultEntity salaListSave(@RequestBody CarListDto carListDto){

        log.info("对象：{}"+carListDto);
        //获取用户名，根据用户名查询用户对象
        String userName = carListDto.getUserName();
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("user_name",userName);
        //用户信息
        TUser tUser = itUserService.selectOne(wrapper);

        //添加销售表
        TSaleList tSaleList = new TSaleList();
        BeanUtils.copyProperties(carListDto,tSaleList);
        tSaleList.setUserId(tUser.getId());
        itSaleListService.save(tSaleList);

        List<SalaListDto> list = carListDto.getList();

        //遍历进货信息 添加到表
        for (SalaListDto listGoods : list ) {
            TSaleListGoods tSaleListGoods = new TSaleListGoods();
            tSaleListGoods.setCode(listGoods.getCode());
            tSaleListGoods.setGoodsId(listGoods.getId());
            tSaleListGoods.setSaleListId(tSaleList.getId());
            tSaleListGoods.setModel(listGoods.getModel());
            tSaleListGoods.setName(listGoods.getName());
            tSaleListGoods.setNum(listGoods.getNum());
            tSaleListGoods.setPrice(listGoods.getSellingPrice());
            tSaleListGoods.setTotal(listGoods.getNum()*listGoods.getSellingPrice());
            tSaleListGoods.setTypeId(listGoods.getTypeId());
            tSaleListGoods.setUnit(listGoods.getUnit());

            itSaleListGoodsService.insert(tSaleListGoods);
        }

        return ResultEntity.ok();
    }




}
